package com.ruoyi.cms.service.impl;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.cms.domain.CmsSiteBannerr;
import com.ruoyi.cms.domain.CmsSiteHero;
import com.ruoyi.cms.domain.vo.CmsSiteAppVo;
import com.ruoyi.cms.domain.vo.CmsSiteWebsitVo;
import com.ruoyi.cms.service.ICmsSiteHeroService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.cms.mapper.CmsSiteAppMapper;
import com.ruoyi.cms.domain.CmsSiteApp;
import com.ruoyi.cms.service.ICmsSiteAppService;

/**
 * APP开发-模块信息Service业务层处理
 *
 * @author ruoyi
 * @date 2022-08-31
 */
@Service
public class CmsSiteAppServiceImpl implements ICmsSiteAppService
{
    @Autowired
    private CmsSiteAppMapper cmsSiteAppMapper;

    @Autowired
    private ICmsSiteHeroService cmsSiteHeroService;

    /**
     * 查询APP开发-模块信息
     *
     * @param id APP开发-模块信息主键
     * @return APP开发-模块信息
     */
    @Override
    public CmsSiteApp selectCmsSiteAppById(Long id)
    {
        return cmsSiteAppMapper.selectCmsSiteAppById(id);
    }

    /**
     * 查询APP开发-模块信息列表
     *
     * @param cmsSiteApp APP开发-模块信息
     * @return APP开发-模块信息
     */
    @Override
    public List<CmsSiteApp> selectCmsSiteAppList(CmsSiteApp cmsSiteApp)
    {
        return cmsSiteAppMapper.selectCmsSiteAppList(cmsSiteApp);
    }

    /**
     * 新增APP开发-模块信息
     *
     * @param cmsSiteApp APP开发-模块信息
     * @return 结果
     */
    @Override
    public int insertCmsSiteApp(CmsSiteApp cmsSiteApp)
    {
        return cmsSiteAppMapper.insertCmsSiteApp(cmsSiteApp);
    }

    /**
     * 修改APP开发-模块信息
     *
     * @param cmsSiteApp APP开发-模块信息
     * @return 结果
     */
    @Override
    public int updateCmsSiteApp(CmsSiteApp cmsSiteApp)
    {
        return cmsSiteAppMapper.updateCmsSiteApp(cmsSiteApp);
    }

    /**
     * 批量删除APP开发-模块信息
     *
     * @param ids 需要删除的APP开发-模块信息主键
     * @return 结果
     */
    @Override
    public int deleteCmsSiteAppByIds(Long[] ids)
    {
        return cmsSiteAppMapper.deleteCmsSiteAppByIds(ids);
    }

    /**
     * 删除APP开发-模块信息信息
     *
     * @param id APP开发-模块信息主键
     * @return 结果
     */
    @Override
    public int deleteCmsSiteAppById(Long id)
    {
        return cmsSiteAppMapper.deleteCmsSiteAppById(id);
    }

    @Override
    public List<CmsSiteAppVo> getAllInfo() {

        List<CmsSiteApp> cmsSiteApps = this.selectCmsSiteAppList(null);
        List<CmsSiteHero> cmsSiteHeroes = cmsSiteHeroService.selectCmsSiteHeroList(null);

        //封装数据
        List<CmsSiteAppVo> collect = cmsSiteApps.stream().map(module -> {
            CmsSiteAppVo cmsSiteWebsitVo = new CmsSiteAppVo();
            BeanUtils.copyProperties(module, cmsSiteWebsitVo);
            List<CmsSiteHero> cmsSiteBanners1 = new ArrayList<>();
            cmsSiteHeroes.stream().forEach(banner -> {
                if (module.getId().equals(banner.getAppId())) {
                    cmsSiteBanners1.add(banner);
                }
            });
            //放之前排序
            cmsSiteBanners1.sort(Comparator.comparing(CmsSiteHero::getSort));
            cmsSiteWebsitVo.setCmsSiteHeroes(cmsSiteBanners1);
            return cmsSiteWebsitVo;
        }).collect(Collectors.toList());

        //返回前排序
        collect.sort(Comparator.comparing(CmsSiteAppVo::getSort));

        return collect;
    }
}
